window.onload = function(){
        //能量值经验条
        function experience(molecule,denominator){//分子分母
            var exp = document.getElementById("exp");

            exp.style.width = molecule/denominator*100 + "%";
            if(molecule/denominator>=1){
                exp.style.width = "100%";
            }
            exp.innerHTML = molecule+"/"+denominator;
        }
        //绑定每个button
        var btnIndex = document.getElementById("information");
        var btnTree = document.getElementById("missionTree");
        var btnMis = document.getElementById("missionHub");
        var c1 = document.getElementById("containerIndex");
        var c2 = document.getElementById("containerTree");
        var c3 = document.getElementById("containerMission");
        btnIndex.onclick = function(){
            c1.style.display = "block";
            c2.style.display = "none";
            c3.style.display = "none";
        }
        btnTree.onclick = function(){
            c1.style.display = "none";
            c2.style.display = "block";
            c3.style.display = "none";
        }
        btnMis.onclick = function(){
            c1.style.display = "none";
            c2.style.display = "none";
            c3.style.display = "block";
        }
        //任务按钮绑定
        var missionR = document.getElementById("missionRev");
        var missionP = document.getElementById("missionPub");
        var hid = document.getElementsByClassName("hid");
        var icon = document.getElementsByClassName("fa-chevron-right");
        missionR.onclick = function(){
            if(hid[0].style.display =="block"){
                hid[0].style.display = "none";
                icon[0].style.transform = 'rotate(0deg)';
            } else {
                hid[0].style.display = "block";
                icon[0].style.transform = 'rotate(90deg)';
            }
        }
        missionP.onclick = function(){
            if(hid[1].style.display =="block"){
                hid[1].style.display = "none";
                icon[1].style.transform = 'rotate(0deg)';
            } else {
                hid[1].style.display = "block";
                icon[1].style.transform = 'rotate(90deg)';
            }
        }
        //更改个人信息的函数
        function changeInformation(userName,schoolName,facultyName,energyNum,energyNumMax){
            var name = document.getElementById("userName");
            var school = document.getElementsByClassName("school");
            var faculty = document.getElementById("faculty");
            var faculity = document.getElementById('faculity');
            var userName1 = document.getElementById('name');
            school[0].innerHTML = schoolName;
            school[1].innerHTML = schoolName;
            name.innerHTML = userName;
            userName1.innerHTML = userName;
            faculty.innerHTML = facultyName;
            faculity.innerHTML = facultyName;
            experience(energyNum,energyNumMax);
        }
        //添加已接受任务
        function appendMissionRev(missionName,missionTime){
            var missions = document.createElement("div");
            missions.className = "missions";
            var p1 = document.createElement("p");
            var p2 = document.createElement("p");
            p1.className = "missionTime";
            p2.className = "missionName";
            p1.innerHTML = missionTime;
            p2.innerHTML = missionName;
            missions.appendChild(p1);
            missions.appendChild(p2);
            hid[0].appendChild(missions);
        }
        //添加发布的任务函数
        function appendMissionPub(missionName,missionTime){
            var missions = document.createElement("div");
            missions.className = "missions";
            var p1 = document.createElement("p");
            var p2 = document.createElement("p");
            p1.className = "missionTime";
            p2.className = "missionName";
            p1.innerHTML = missionTime;
            p2.innerHTML = missionName;
            missions.appendChild(p1);
            missions.appendChild(p2);
            hid[1].appendChild(missions);
            /*
            myAppendChildClass(missions,'p',"missionTime",missionTime);
            myAppendChildClass(missions,'p',"missionName",missionName);
            */

        }
        function getCookie(cookieName) {
            var allcookies = document.cookie;
            var cookiePos = allcookies.indexOf(cookieName); //索引的长度
            if (cookiePos != -1) {
                // 把cookie_pos放在值的开始，只要给值加1即可。
                cookiePos += cookieName.length + 1;
                var cookieEnd = allcookies.indexOf(";", cookiePos);

                if (cookieEnd == -1) {
                    cookieEnd = allcookies.length;
                }
                var value = decodeURI(allcookies.substring(cookiePos, cookieEnd));
            }
            return value;
        }
        //运行函数
        var name=getCookie("Name");
        var phone=getCookie("Phone");
        var number=getCookie("Number");
        var faculty=getCookie("Faculty");
        var identity=getCookie("Identity");
        var school=getCookie("School");
        var energy=getCookie("Energy");
        changeInformation(name,school,faculty,energy,"100");
        var data= {
            number:sessionStorage.getItem("number")
        };
        $.ajax({
            type:"post",
            dataType:"json",
            url:"/fp_war_exploded/Information",
            data:JSON.stringify(data),
            success: function(result) {
                // alert("获取成功!"+result.password+result.missions[0].author);
                var i;
                var hid1 = document.getElementById("hid1");
                var hid2 = document.getElementById("hid2");
                //hid1已接受/hid2已发布
                for(i=0;i<result.missions.length;i++){
                    if((result.missions[i].recipient==result.number)&&(result.missions[i].author!==result.name)){
                        appendMission(hid1,result.missions[i].id,result.missions[i].title,result.missions[i].createTime,result.missions[i].content,result.number,result.missions[i].author,result.missions[i].recipient,result.missions[i].place,result.missions[i].reward);
                        (document.getElementById(result.missions[i].id).parentNode).addEventListener("click",missionclick1,false);
                    }else if (result.missions[i].author==result.name&&result.missions[i].recipient!==result.number)
                    {
                        appendMission(hid2,result.missions[i].id,result.missions[i].title,result.missions[i].createTime,result.missions[i].content,"",result.missions[i].author,result.missions[i].recipient,result.missions[i].place,result.missions[i].reward);
                        (document.getElementById(result.missions[i].id).parentNode).addEventListener("click",missionclick2,false);

                    }
                }
                function missionclick1(){
                    show.appendChild(title("div",this));
                    document.body.style.overflow = "hidden";
                    elasticFrame.style.display = "block";
                    var failedBtn = document.getElementsByClassName("failedBtn")[0];
                    failedBtn.onclick = function(){
                        // console.log(failedBtn.id);
                        var data1={
                            id:failedBtn.id
                        }
                        $.ajax({
                            type:"post",
                            dataType:"text",
                            url:"/fp_war_exploded/quit",
                            data:JSON.stringify(data1),
                            success:function (result) {
                                if(result.match("success")){
                                    alert("放弃");
                                    window.location.href="index.html";
                                }
                            },
                            error:function () {
                                alert("error");
                            }
                        });
                    }
                }
                function missionclick2(){
                    show2.appendChild(title("div",this));
                    document.body.style.overflow = "hidden";
                    elasticFrame2.style.display = "block";
                    var seccessBtn = document.getElementsByClassName("seccessBtn")[0];
                    seccessBtn.onclick = function(){
                        // console.log(seccessBtn.id);
                        var data2={
                            id:seccessBtn.id
                        }
                        $.ajax({
                            type:"post",
                            dataType:"text",
                            url:"/fp_war_exploded/achieve",
                            data:JSON.stringify(data2),
                            success:function (result) {
                                if(result.match("success")){
                                    alert("完成成功");
                                    window.location.href="selfInformation.html";
                                }
                            },
                            error:function () {
                                alert("销毁成功");
                                window.location.href="selfInformation.html";
                            }
                        });
                    }
                }

            },
            error:function () {
                alert("hahahahah");
            }
        });


    }